import { ElMessage } from "element-plus"
import { showToast } from "vant"
import { Utils } from "./utils"
import { RELOADDINGTIME } from "../enum";

const isPhone = (window as any).isPhone
class Timber {
    timber: Set<string>;
    constructor() {
        this.timber = new Set()
    }
    deleteItem(item: string) {
        this.timber.delete(item)
    }
    addItem(item: string) {
        this.timber.add(item)
    }
    hasItem(item: string) {
        return this.timber.has(item)
    }
}
const store = new Map()
//防止同一个接口多次访问
const timer = new Timber()
export class Http extends Utils {
    _gettoken() {
        return localStorage.getItem('token') || ''
    }
    async request(url: string, {
        headers, method, data, parames, unShowToast, unLoadding, cache
    }: { headers?: HeadersInit, method?: string, data?: any, parames?: any, unShowToast?: boolean, unLoadding?: boolean, cache?: boolean } = {}) {
        const Url = '/api' + url + this.utils_param(parames)
        if (cache && store.has(Url)) return store.get(Url)
        if (timer.hasItem(Url)) {
            return console.error('请求重复')
        } else {
            // console.log(Url + ' body:' + JSON.stringify(data))
        }
        const header: HeadersInit & { Authorization?: string } = { 'Content-Type': 'application/json', 'accept': 'application/json', ...headers }
        const token = this._gettoken()
        if (token) header.Authorization = 'Bearer ' + token
        const promise = fetch(Url, {
            headers: header,
            method: method || "POST", body: JSON.stringify(data),
        });
        timer.addItem(Url);
        const res = await promise;
        timer.deleteItem(Url)
        if (res.status == 200) {
            const data = await res.json()
            const msg = data.response
            const errorCode = data.errorCode
            if (true) {
                if (cache) {
                    store.set(Url, msg)
                    setTimeout(() => {
                        store.delete(Url)
                    }, RELOADDINGTIME);
                }
                if (errorCode === 10016) {
                    // await adminStore().reLogin(data.token)
                }
                return msg
            } else {
                if (errorCode === 10006) {
                    // adminStore().logOut()
                    return 'unlogin'
                }
                if (unShowToast) return
                if (Array.isArray(msg)) {
                    if (isPhone) {
                        showToast(msg[0])
                    } else {
                        ElMessage.error(msg[0])
                    }
                } else {
                    if (isPhone) {
                        showToast(msg)
                    } else {
                        ElMessage.error(msg)
                    }
                }
            }
        } else {
            if (isPhone) {
                showToast('服务器异常')
            } else {
                ElMessage.error('服务器异常')
            }
        }
    }
}